
package com.xinan.xs;

import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xinan.hygl.Hygl;
import com.xinan.utils.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HyglXfController extends BaseController {

    public void index() {
        render("/WEB-INF/views/xfjz/index.html");
    }
    @Before(SearchSql.class)
    public void xfjz() {
    	List<Record> newlist = null;
        List<Record> hylist = null;
        List<Record> findmoey = null;
        String card = getPara("card");
        String where2 = "card='"+card+"' or yhm='"+card+"' or dh='"+card
				+"' or cph='"+card+"' or cph2='"+card+"'";
        String where = " s.card=h.card and (h.card='"+card+"' or h.yhm='"+card+"' or h.dh='"+card
        		+"' or h.cph='"+card+"' or h.cph2='"+card+"') and s.yxbz=0 group by s.card";
        String where3 = " s.card=h.card and (h.card='"+card+"' or h.yhm='"+card+"' or h.dh='"+card
        		+"' or h.cph='"+card+"' or h.cph2='"+card+"') and s.yxbz=0 group by s.card";
        if(StrKit.isBlank(card)){
        	where = " s.yxbz=0 group by s.card";
        	where3 = " s.yxbz=0 group by s.card";
        	hylist = Hygl.dao.findUnPayHY(" s2.card=s.card and s2.yxbz=0");
        	findmoey = HyglXf.dao.findmoey(where3);
        }else{
        	hylist = Hygl.dao.findAlls(where2);
        	findmoey = HyglXf.dao.findmoeyByHYinfo(where3);
        }
        newlist = HyglXf.dao.findByHYinfo(where);
        
        setAttr("newlist", newlist);
        setAttr("findmoey", findmoey);
        setAttr("hylist", hylist);
        render("/WEB-INF/views/xfjz/form.html");
    }

    /**
     * add
     */
    public void newModes() {
        String id = getPara("id");
//    List<Record> newlist = Db.find("select  * from     where    id=  '" + id + "'");
        List<Record> newlist = HyglXf.dao.findAlls("       id=  '" + id + "'");

        setAttr("newlist", newlist);
        render("test/hyglXf.html");
    }

    /**
     * 在增加了SearchSql拦截之后就可以根据页面配置信息
     * 自动装配查询语句放入request中,通过getAttr(Const.SEARCH_SQL)函数获得
     * 根据实际需要自动组装到Sql语句里面
     */
    @Before(SearchSql.class)
    public void query() {
        int pageNumber = getParaToInt("page");
        int pageSize = getParaToInt("rows");
        String where = getAttr(Const.SEARCH_SQL);
        Page<HyglXf> page = HyglXf.dao.page(pageNumber, pageSize, where);
        Map root = new HashMap();
        /// jqgrid
        root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		root.put("records", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
    }

    /**
     * save
     *
     * @param
     * @return
     */
    @Before(Tx.class)
    public void save() {
        String card=getPara("cards");
        String ida=getPara("ida");
        String vipcard=getPara("vipcard");
        String moneys=getPara("moneys");
        String xfsjjga=getPara("xfsjjga");
        String ye=getPara("ye");
        String jzrq="";
        String czrydm = getAttr(Constants.USER_NAME);
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        jzrq = sdf.format(dt);
        Record xx=new Record();
        xx.set("card",card).set("ida", ida).set("vipcard", vipcard).set("moneys", moneys).set("xfsjjga", xfsjjga)
                .set("jzrq", jzrq).set("czrydm",czrydm) ;
        Boolean cont=Db.save("hygl_jz",xx);
//        Long cardmoney;
//        if(vipcard==""){
//            vipcard="0";
//        }
//        if(ye==""){
//            cardmoney  =0- Long.parseLong(vipcard);
//        } else {
//             cardmoney  =  Long.parseLong(ye) -  Long.parseLong(vipcard);
//        }
//        int j =Db.update("update hygl set ye="+cardmoney+"  where card= '" + card + "'");
//        if(j>0 ){
//            cont =true;
//        } else {
//            cont =false;
//        }
        int k=Db.update("update hygl_xf set yxbz=1  where id in (" + ida + ")");
        Db.update("insert into cpgl_ck ( wpid,cksl,ckje,ckyy) select s.xfid  ,s.xfsl , s.xfje,'1'  from hygl_xf  s where s.id in (" + ida + ")   and  xfbz='sptj' ");
        if(k>0 ){
            cont =true;
        } else {
            cont =false;
        }
        if (cont) {
        	renderText(card);
        } else {
        	renderText("0");
        }
    }

    /**
     * Post请求的菜单更新函数,可以接受菜单的id和菜单对象,并且根据菜单id进行更新,返回影响行数根据此判断是否操作成功
     * 本例子使用rose框架的est风格url可以吧参数作为路径一部分进行传递
     *
     * @param
     * @return
     */
    public void update() {
        if (getModel(HyglXf.class).update()) {
        	renderText(Constants.EDITSUCCESS);
        } else {
        	renderText(Constants.EDITFAILED);
        }
    }

    /**
     * Post请求的菜单删除函数,可以接受菜单ID 并且根据ID进行物理删除,并且返回影响行数,据此判断是否操作成功
     *
     * @param
     * @return
     */
    public void delete() {
        String ida=getPara("ida");
        String cards=getPara("cards");

        String[] id=ida.split(",");
        Boolean boo=true;
        boolean boos=false;
        boolean booa=false;
        for (int i=0;i<id.length;i++){
            boos= Db.deleteById("hygl_xf",id[i]);
            booa=  boo &&boos;
        }
        if (booa) {
        	renderText(cards);
        } else {
        	renderText("0");
        }
    }
}

